<template>
  <el-form :model="nodeModalForm" ref="nodeModal" label-placement="left" label-width="100px" :rules="nodeModalRules"
           size="small">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model.trim="nodeModalForm.nodeName"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <table border="1" align="center" class="add_table w_100" cellspacing="0">
      <tr>
        <th width="70"><span class="iconfont icon-Add cursor_point" @click="handleAddOrder"></span></th>
        <th>字段</th>
        <th>排序方式</th>
      </tr>
      <tr v-for="(item,index) in nodeModalForm.order" :key="index">
        <td width="70" align="center">
          <span class="iconfont icon-Add cursor_point px_2" @click="handleAddOrder(index)"></span>
          <span class="iconfont icon-close cursor_point px_2" @click="handleDeleteOrder(index)"></span>
        </td>
        <td>
          <el-select v-model="item.field" clearable filterable placeholder="字段" class="w_100">
            <el-option v-for="item in rowsList" :key="item.name" :label="item.alias" :value="item.alias"></el-option>
          </el-select>
        </td>
        <td>
          <el-select v-model="item.order" filterable placeholder="字段" class="w_100">
            <el-option v-for="item in orderList" :key="item.label" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </td>
      </tr>
    </table>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </el-form>
</template>

<script>
import { computed, defineComponent, inject, onMounted, ref } from "vue";
import { useEtlStore } from "../../../../../store/flowEtl"

export default defineComponent({
  name: "node-config-order",
  props: {
    node: Object,
  },
  components: {},
  setup (props, ctx) {
    const etlStore = useEtlStore();
    const rowsList = ref([]);
    const orderList = ref([{
      value: "DESC",
      label: "倒序"
    }, {
      value: "ASC",
      label: "正序"
    }])
    const nodeList = computed(() => etlStore.nodeList);
    const nodeConfigClose = inject("nodeConfigClose");
    const nodeModalForm = ref({
      nodeName: "",
      order: [],
    });
    const nodeModal = ref();
    const tableLoading = ref(false);
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入数据源名称",
        trigger: "blur",
      }
    });
    const handleConfigCancel = () => {
      nodeConfigClose();
    };
    const nodeConfigUpdate = (node) => {
      let nodes = [...nodeList.value].map(item => {
        if (item.id === node.id) {
          item.status = true;
          item.nodeName = node.nodeName;
          item.config.order = node.order;
        }
        return item;
      });
      etlStore.setNodeList(nodes);
      nodeConfigClose();
    };
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value);
        } else {
          return false;
        }
      });
    };
    const resetForm = () => {
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        order: []
      };
    };
    const handleAddOrder = (index) => {
      if (index) {
        nodeModalForm.value.order = [...nodeModalForm.value.order.splice(0, index + 1), {
          field: "", order: "ASC"
        }, ...nodeModalForm.value.order.splice(index - 1, nodeModalForm.value.order.length)]
      } else {
        nodeModalForm.value.order.push({
          field: "", order: "ASC"
        })
      }
    };
    const handleDeleteOrder = (index) => {
      nodeModalForm.value.order.splice(index, 1);
    };
    onMounted(() => {
      resetForm();
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName,
        order: props.node.config.order,
      };
      rowsList.value = props.node.originFields;
    });
    return {
      nodeList,
      tableLoading,
      nodeModalForm,
      nodeModalRules,
      nodeModal,
      rowsList,
      orderList,
      resetForm,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      handleAddOrder,
      handleDeleteOrder,
      nodeConfigClose,
    };
  },
});
</script>

<style lang="less" scoped>
.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}
</style>